package com.zc.finkDemo.service;

import com.zc.finkDemo.entities.RelationInfo;
import com.zc.finkDemo.mapper.RelationMapper;

import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;

public class RelationService {

    //缓存 如果业务允许  可以考虑引入caffine加入过期时间
    public static ConcurrentHashMap<String, RelationInfo> cache = new ConcurrentHashMap<>();


    /**
     * 根据规则key获取规则内容
     * 先从本地缓存中获取
     * 如果获取不到再从数据库中查询 并放入缓存
     *
     * @param key
     * @return
     */
    public static RelationInfo getRelation(String key) throws SQLException {
        RelationInfo relationInfo = cache.get(key);
        if (relationInfo == null) {
            relationInfo = RelationMapper.getRelation(key);
            cache.put(key, relationInfo);
        }
        return relationInfo;
    }
}
